Introduction and Background Inheritance is an effective clas
Introduction and Background
 Inheritance is an effective class building tool in object-oriented programming. Through inheritance, a new class (or subclass) can obtain all of the data and methods declared in a previously created class (or superclass) that it \"extends\". If the data in the superclass is declared to be protected (or public), the subclass can also access / manipulate the data directly within its own methods. Before completing this lab, review the material on inheritance discussed in lecture and also provided in the course slides and in the textbook. Note: Inheritance is first covered in Lecture 18 – thus if you are reading this lab in advance you may not yet have had a lecture on inheritance. However, you should still be able to figure it out if you read the notes and look over the handouts.
 MySquare Class
 In Lab 5 you completed the MyRectangle class, which is a simple representation of a rectangle. Review Lab 5 before completing this lab and retrieve the MyRectangle class that you completed for that lab, since you will need it again in this lab. Before starting this lab, edit your MyRectangle class in the following way: change the declaration of your instance variables from private to protected. This will enable access of these variables by your MySquare subclass.
 In this lab you will implement a new class with the following header:
 public class MySquare extends MyRectangle
 Your MySquare class should NOT have any new instance variables. If you think about it you will see that the instance variables already present in MyRectangle are sufficient. However, you will need some new methods in your MySquare class. Specifically:
 public MySquare()
 public MySquare(int x, int y, int side)
 // Constructors to allow new objects to be created
 public String toString()
 // Redefining toString(). See output for effect.
 public void setSize(int w, int h)
 // Redefining setSize(). This must be done because the inherited version
 // allows the width and height to differ but in a square they must be the
 // same. In this version, if the width and height are not the same, the
 // method should output a message and not change anything.
 public void setSide(int side)
 // This is a new method that updates that size of the square. Think about
 // how this will be implemented using the existing inherited instance vars.
 Your MySquare class must run with the Lab8.java (http://people.cs.pitt.edu/~ramirez/cs401/labs/Lab8.java) program with no changes. To see how your output should look see the file lab8out.txt (http://people.cs.pitt.edu/~ramirez/cs401/labs/lab8out.txt). Note that there are some methods that are utilized in Lab8.java which are inherited and which you should not redefine in your MySquare class. See details in the comments in Lab8.java.
 Grading and Submission
 Demonstrate that your MySquare class works correctly by running the Lab8.java program for your TA. Be ready to show your code to your TA as well.
 There will be 4 raw points for this lab, broken down in the following way.
 Constructors: 1 point
 toString() method: 1 point
 setSide() method: 1 point
 setSize() method: 1 point
 Note that your program must work with the provided driver program in order to receive credit for these items.
 Introduction and Background
 Inheritance is an effective class building tool in object-oriented programming. Through inheritance, a new class (or subclass) can obtain all of the data and methods declared in a previously created class (or superclass) that it \"extends\". If the data in the superclass is declared to be protected (or public), the subclass can also access / manipulate the data directly within its own methods. Before completing this lab, review the material on inheritance discussed in lecture and also provided in the course slides and in the textbook. Note: Inheritance is first covered in Lecture 18 – thus if you are reading this lab in advance you may not yet have had a lecture on inheritance. However, you should still be able to figure it out if you read the notes and look over the handouts.
 MySquare Class
 In Lab 5 you completed the MyRectangle class, which is a simple representation of a rectangle. Review Lab 5 before completing this lab and retrieve the MyRectangle class that you completed for that lab, since you will need it again in this lab. Before starting this lab, edit your MyRectangle class in the following way: change the declaration of your instance variables from private to protected. This will enable access of these variables by your MySquare subclass.
 In this lab you will implement a new class with the following header:
 public class MySquare extends MyRectangle
 Your MySquare class should NOT have any new instance variables. If you think about it you will see that the instance variables already present in MyRectangle are sufficient. However, you will need some new methods in your MySquare class. Specifically:
 public MySquare()
 public MySquare(int x, int y, int side)
 // Constructors to allow new objects to be created
 public String toString()
 // Redefining toString(). See output for effect.
 public void setSize(int w, int h)
 // Redefining setSize(). This must be done because the inherited version
 // allows the width and height to differ but in a square they must be the
 // same. In this version, if the width and height are not the same, the
 // method should output a message and not change anything.
 public void setSide(int side)
 // This is a new method that updates that size of the square. Think about
 // how this will be implemented using the existing inherited instance vars.
 Your MySquare class must run with the Lab8.java (http://people.cs.pitt.edu/~ramirez/cs401/labs/Lab8.java) program with no changes. To see how your output should look see the file lab8out.txt (http://people.cs.pitt.edu/~ramirez/cs401/labs/lab8out.txt). Note that there are some methods that are utilized in Lab8.java which are inherited and which you should not redefine in your MySquare class. See details in the comments in Lab8.java.
 Grading and Submission
 Demonstrate that your MySquare class works correctly by running the Lab8.java program for your TA. Be ready to show your code to your TA as well.
 There will be 4 raw points for this lab, broken down in the following way.
 Constructors: 1 point
 toString() method: 1 point
 setSide() method: 1 point
 setSize() method: 1 point
 Note that your program must work with the provided driver program in order to receive credit for these items.
 Introduction and Background
 Inheritance is an effective class building tool in object-oriented programming. Through inheritance, a new class (or subclass) can obtain all of the data and methods declared in a previously created class (or superclass) that it \"extends\". If the data in the superclass is declared to be protected (or public), the subclass can also access / manipulate the data directly within its own methods. Before completing this lab, review the material on inheritance discussed in lecture and also provided in the course slides and in the textbook. Note: Inheritance is first covered in Lecture 18 – thus if you are reading this lab in advance you may not yet have had a lecture on inheritance. However, you should still be able to figure it out if you read the notes and look over the handouts.
 MySquare Class
 In Lab 5 you completed the MyRectangle class, which is a simple representation of a rectangle. Review Lab 5 before completing this lab and retrieve the MyRectangle class that you completed for that lab, since you will need it again in this lab. Before starting this lab, edit your MyRectangle class in the following way: change the declaration of your instance variables from private to protected. This will enable access of these variables by your MySquare subclass.
 In this lab you will implement a new class with the following header:
 public class MySquare extends MyRectangle
 Your MySquare class should NOT have any new instance variables. If you think about it you will see that the instance variables already present in MyRectangle are sufficient. However, you will need some new methods in your MySquare class. Specifically:
 public MySquare()
 public MySquare(int x, int y, int side)
 // Constructors to allow new objects to be created
 public String toString()
 // Redefining toString(). See output for effect.
 public void setSize(int w, int h)
 // Redefining setSize(). This must be done because the inherited version
 // allows the width and height to differ but in a square they must be the
 // same. In this version, if the width and height are not the same, the
 // method should output a message and not change anything.
 public void setSide(int side)
 // This is a new method that updates that size of the square. Think about
 // how this will be implemented using the existing inherited instance vars.
 Your MySquare class must run with the Lab8.java (http://people.cs.pitt.edu/~ramirez/cs401/labs/Lab8.java) program with no changes. To see how your output should look see the file lab8out.txt (http://people.cs.pitt.edu/~ramirez/cs401/labs/lab8out.txt). Note that there are some methods that are utilized in Lab8.java which are inherited and which you should not redefine in your MySquare class. See details in the comments in Lab8.java.
 Grading and Submission
 Demonstrate that your MySquare class works correctly by running the Lab8.java program for your TA. Be ready to show your code to your TA as well.
 There will be 4 raw points for this lab, broken down in the following way.
 Constructors: 1 point
 toString() method: 1 point
 setSide() method: 1 point
 setSize() method: 1 point
 Note that your program must work with the provided driver program in order to receive credit for these items.
Solution
public class MySquare extends MyRectangle{ public MySquare() { super(); } public MySquare(int width, int height, int side) { super(width, height, side); } public void setSize(int w, int h){ if(w != h) System.out.println(\"Error. Width and height are not same\"); else{ width = w; height = h; } } public void setSide(int side){ this.side = side; } public String toString(){ return \"Width: \" + width + \", Height: \" + height + \", Side: \" + side; } }


